// pages/user/shop-order-list/add-work-order.js
const app = getApp()
const user  = require('../../../api/user')
const global = require('../../../api/global')
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
import Dialog from '../../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    user: app.getUser(),
    goodsId: '',
    goodsPrice: '',
    goodsNum: '',
    categoryColumns: [],
    typeColumns: ['请选择售后种类'],
    showCategory: false,
    showType: false,
    categoryIndex: -1,
    categoryLabel: '请选择售后种类',
    typeLabel: '请选择售后原因',
    typeIndex: -1,
    remark: '',
    images: [],
    refundPrice:'',
    refundNum: 1,
    textareaHeight: {
      maxHeight: 100
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const orderId = options.orderId
    const goodsId = options.goodsId
    const goodsPrice = options.goodsPrice
    const goodsNum = options.goodsNum
    this.setData({orderId,goodsId,goodsPrice,goodsNum,refundPrice:goodsPrice})
    this.getCategory()
  },
  
  imageAfterRead(e) {
    const _this = this
    const {file} = e.detail
    console.log('file,,',file)
    
    wx.uploadFile({
      url: app.globalData.deployUrl + global.UPLOAD_FILE, 
      filePath: file.path,
      name: 'file',
      success(res) {
        const data = JSON.parse(res.data)
        if (data.code === 0) {
          
          _this.setData({ images: _this.data.images.concat({url: data.key}) })
        } else{
          Dialog.alert({title: '提示', message: data.msg});
        }
        
      },
      fail(err) {
        Dialog.alert({title: '提示', message: '错误'});
      }
    });
    
  },

  getCategory() {
    app.requestUrl({url: user.WORKORDER_CATEGORY}).then(res => {
      let category = res.data.data;
      let categoryColumns = []
      category.forEach(item => {
        categoryColumns.push({
          id: item.id,
          text: item.typename
        })
      })
      this.setData({categoryColumns})
    })
  },

  onCategory(e) {
    console.log(e)
    const {index,value} = e.detail
    this.setData({
      showCategory: false,
      categoryIndex: index,
      categoryLabel: this.data.categoryColumns[index].text || '',
      typeLabel: '请选择售后原因',
      typeIndex: -1,
      typeColumns: []
    })

    app.requestUrl({url: user.WORKORDER_TYPE, data: {
      category: value.id
    }}).then(res => {
      let type = res.data.data;
      let typeColumns = []
      type.forEach(item => {
        typeColumns.push({
          id: item.id,
          text: item.cause
        })
      })
      this.setData({typeColumns})
    })
  },

  onType(e) {
    const {index,value} = e.detail
    if (this.data.typeColumns.length > 0) {
      this.setData({typeIndex: index,typeLabel: value.text})
      
    } else {
      Toast('请先选择售后种类');
    }
    this.setData({showType: false})
  },
  onRefundNum(e) {

      this.setData({refundPrice: (e.detail* this.data.goodsPrice).toFixed(2),refundNum: e.detail})
      // this.refundPrice = this.refundNum * this.goodsPrice;
  },
  onRefundPriceChange (e) { 
    const total = (this.data.goodsPrice * this.data.refundNum).toFixed(2)
    this.setData({refundPrice: e.detail > total ? total : e.detail})
  },
  onRemarkChange (e) { 
    this.setData({remark: e.detail})
  },
  saveWorkOrder() { 
     if (this.data.categoryIndex < 0 || this.data.typeIndex < 0) {
      return Dialog.alert({title: '提示', message: '请选择售后种类和原因'});
     }
     if (!this.data.images.length) {
      return Dialog.alert({title: '提示', message: '请您上传图片'});
     }

     let pic = '';
     this.data.images.forEach(img => {
         pic += img.url + ',';
     });

     app.requestUrl({url:user.WORKORDER_ADD, data: {
          mid: this.data.user.id,
          oid: this.data.orderId,
          ogid: this.data.goodsId,
          type: this.data.categoryColumns[this.data.categoryIndex].id,
          prid: this.data.typeColumns[this.data.typeIndex].id,
          contents: this.data.remark,
          pic: pic,
          nums: this.data.refundNum,
          money: this.data.refundPrice
      }}).then(res => {
          if (res.data.data > 0) {
            Toast.success("工单提交成功");
              //this.$router.go(-1);
              wx.navigateBack()
          } else {
            Toast.fail("工单提交失败");
          }
      });
  },

  showCategory() {
    this.setData({showCategory: true})
  },

  hideCategory() {
    this.setData({showCategory: false})
  },

  showType() {
    this.setData({showType: true})
  },

  hideType() {
    this.setData({showType: false})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})